home *** CD-ROM | disk | FTP | other *** search
/ Programming Windows (5th Edition) / Programming Windows, 5th ed. - Companion CD (097-0002183)(1999).iso / Chap16 / Tunnel / Tunnel.c next >
Encoding:
C/C++ Source or Header  |  1998-10-09  |  2.5 KB  |  94 lines

  1. /*---------------------------------------
  2.    TUNNEL.C -- Palette Animation Demo
  3.                (c) Charles Petzold, 1998
  4.   ---------------------------------------*/
  5.  
  6. #include <windows.h>
  7.  
  8. #define ID_TIMER 1
  9.  
  10. TCHAR szAppName [] = TEXT ("Tunnel") ;
  11. TCHAR szTitle   [] = TEXT ("Tunnel: Palette Animation Demo") ;
  12.  
  13. static LOGPALETTE * plp ;
  14.  
  15. HPALETTE CreateRoutine (HWND hwnd)
  16. {
  17.      BYTE     byGrayLevel ;
  18.      HPALETTE hPalette ;
  19.      int      i ;
  20.  
  21.      plp = malloc (sizeof (LOGPALETTE) + 255 * sizeof (PALETTEENTRY)) ;
  22.      
  23.           // Initialize the fields of the LOGPALETTE structure
  24.      
  25.      plp->palVersion    = 0x0300 ;
  26.      plp->palNumEntries = 128 ;
  27.      
  28.      for (i = 0 ; i < 128 ; i++)
  29.      {
  30.           if (i < 64)
  31.                byGrayLevel = (BYTE) (4 * i) ;
  32.           else
  33.                byGrayLevel = (BYTE) min (255, 4 * (128 - i)) ;
  34.           
  35.           plp->palPalEntry[i].peRed   = byGrayLevel ;
  36.           plp->palPalEntry[i].peGreen = byGrayLevel ;
  37.           plp->palPalEntry[i].peBlue  = byGrayLevel ;
  38.           plp->palPalEntry[i].peFlags = PC_RESERVED ;
  39.           
  40.           plp->palPalEntry[i + 128].peRed   = byGrayLevel ;
  41.           plp->palPalEntry[i + 128].peGreen = byGrayLevel ;
  42.           plp->palPalEntry[i + 128].peBlue  = byGrayLevel ;
  43.           plp->palPalEntry[i + 128].peFlags = PC_RESERVED ;
  44.      }
  45.    
  46.      hPalette = CreatePalette (plp) ;
  47.      
  48.      SetTimer (hwnd, ID_TIMER, 50, NULL) ;
  49.      return hPalette ;
  50. }
  51.  
  52. void PaintRoutine (HDC hdc, int cxClient, int cyClient)
  53. {
  54.      HBRUSH hBrush ;
  55.      int    i ;
  56.      RECT   rect ;
  57.      
  58.      for (i = 0 ; i < 127 ; i++)
  59.      {
  60.                // Use a RECT structure for each of 128 rectangles
  61.           
  62.           rect.left   =            i * cxClient / 255 ;
  63.           rect.top    =            i * cyClient / 255 ;
  64.           rect.right  = cxClient - i * cxClient / 255 ;
  65.           rect.bottom = cyClient - i * cyClient / 255 ;
  66.           
  67.           hBrush = CreateSolidBrush (PALETTEINDEX (i)) ;
  68.           
  69.                // Fill the rectangle and delete the brush
  70.           
  71.           FillRect (hdc, &rect, hBrush) ;
  72.           DeleteObject (hBrush) ;
  73.      }
  74.      return ;
  75. }
  76.  
  77. void TimerRoutine (HDC hdc, HPALETTE hPalette)
  78. {
  79.      static int iLevel ;
  80.  
  81.      iLevel = (iLevel + 1) % 128 ;
  82.  
  83.      AnimatePalette (hPalette, 0, 128, plp->palPalEntry + iLevel) ;
  84.      return ;
  85. }
  86.  
  87. void DestroyRoutine (HWND hwnd, HPALETTE hPalette)
  88. {
  89.      KillTimer (hwnd, ID_TIMER) ;
  90.      DeleteObject (hPalette) ;
  91.      free (plp) ;
  92.      return ;
  93. }
  94.